#!/usr/local/bin/python3.7
import pymysql
import time
import sys
import os
import logging
from configparser import ConfigParser
import json
import pandas
import datetime
import random
from DrissionPage import WebPage,ChromiumPage,ChromiumOptions,SessionPage

logging.basicConfig(filename='myapp.log', level=logging.ERROR)
logger = logging.getLogger(__name__)

#path = r'C:\Program Files\Google\Chrome\Application\Chrome.exe'  # 请改为你电脑内Chrome可执行文件路径
#path = r'/usr/bin/chrome-google-stable'  # 请改为你电脑内Chrome可执行文件路径
base_path = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(base_path, 'config.ini')
print('file_path',file_path)
config = ConfigParser()
config.read(file_path)
path = config.get('server','chrome_path')

ChromiumOptions().set_browser_path(path).save()
#co = ChromiumOptions().headless() #无头浏览器
#page = ChromiumPage(co)
# co = ChromiumOptions().set_local_port(9222)
# page = ChromiumPage(addr_or_opts=co)
page = ChromiumPage()
pageSession = SessionPage()

argv_path = sys.argv[0]
if ((os.path.isfile(argv_path))):
    print(argv_path + ' 已找到，开始执行...')
else:
    print('File not exist!!!')
    sys.exit()

startTime = time.time()  # 计时开始
print('计时开始，当前时间为', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(startTime)))

def main():
    targetUrl = config.get('server','target_url')
    # 加载界面
    page.get(targetUrl)

    # 监听数据包特征
    simple = ['/quickbuy/v1/poi/sputag/products', '/quickbuy/v1/poi/product/smooth/render']
    page.listen.start(simple)
    
    wait = config.getint('setting','wait_seconds')
    print(f"进入登录页面等待{wait}s...")
    page.wait(wait)
    
    #截图保存路径
    saveScreeShot()

    res = page.listen.wait()  # 等待并获取一个数据包
    saveRequestHeaders(res.request.headers)
    saveRequestCookies(res.request.cookies)
    savePostData(res.request.postData)

    needData = res.response.body
    saveResponseBody(needData)

    json_string = processData(needData)

    header = [
    '商品ID','商品名称','商品原价','商品现价','会员价','商品图片','月销量','最低购买数量','upccode','库存'
    ]
    # 创建当天目录
    current_directory = formatDate()
    mkdir(current_directory)
    fileName = current_directory + os.sep + generate_random_filename()
    exportExcel(json_string, header, fileName)

def saveScreeShot():
    #截图保存路径
    save_path = config.get('setting', 'login_screenshot_path')
    save_name = config.get('setting', 'login_screenshot_name')
    # 截图保存
    page.get_screenshot(path=save_path, name=save_name, full_page=True)
       
def getconfig(type, key):
    # 创建一个解析器对象
    config = ConfigParser()
    
    # 读取配置文件
    config.read('config.ini')
    return config.get(type, key)
    
def updateMysql(session):
    dbconfig = dict(config.items('database'))
    # 数据库连接
    db = pymysql.connect(**dbconfig)

    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()

    # SQL 更新语句
    sql = "UPDATE ht_set SET `value` = '" + session + "' WHERE `key` = 'cookie'"
    try:
        # 执行SQL语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
    except:
        # 发生错误时回滚
        db.rollback()

    # 关闭数据库连接
    db.close()

def saveRequestHeaders(data):
    with open('headers.txt', 'w') as file:
        for key, value in data.items():
            file.write(f'{key}: {value}\n')

def readRequestHeaders():
    with open('headers.txt', 'r') as file:
        read_data = file.read()
        print(read_data)

def saveRequestCookies(data):
    # 将列表字典转换为JSON格式的字符串
    data_json = json.dumps(data, ensure_ascii=False)
    # 将字符串写入文件
    with open('cookies.txt', 'w') as file:
        file.write(data_json)

def savePostData(dict_data):
    # 写入文件
    with open('postData.txt', 'w', encoding='utf-8') as file:
        file.write(str(dict_data) + '\n')

def saveResponseBody(dict_data):
    # 将字典转换为JSON格式的字符串
    json_string = json.dumps(dict_data, ensure_ascii=False)
    with open('responseBody.txt', 'w', encoding='utf-8') as file:
        file.write(json_string + '\n')

def exportExcel(exdata, exheader, fileName):
    df = pandas.DataFrame(exdata)
    try:
        # 设置Excel文件的中文标题
        df.columns = exheader
        df.to_excel(fileName + '.xlsx', index=False)
    except Exception as e:
        print(f"excel转换出错：{e}")

def mkdir(dir_path):
    if not os.path.exists(dir_path):
        os.mkdir(dir_path)

def generate_random_filename():
    today = formatDate()
    # 生成随机数作为文件名的一部分
    random_number = random.randint(10000000, 99999999)
    # 生成文件名
    filename = f"{today}_{random_number}"
    return filename

def formatDate():
     # 获取当前日期
    now = datetime.datetime.now()
    # 格式化日期为字符串
    return now.strftime("%Y%m%d")

def processData(json_data):
    data = []
    print(json_data['data'])
    for row in json_data['data'].get('product_spu_list', []):
        tmp = {
            "id": row.get('id', ''),# 商品id
            "name": row.get('name', ''),# 商品名称
            "origin_price": row.get('skus', '')[0].get('origin_price'),# 划线价
            "min_price": row.get('min_price', ''),# 现价
            "member_price": row.get('member_price', ''),# 会员价
            "picture": row.get('picture', ''),# 商品图片
            "month_sales": row.get('month_sales', ''),# 月销量
            "min_order_count": row.get('skus', '')[0].get('min_order_count'),# 最低购买数量
            "upccode": row.get('skus', '')[0].get('upccode'),
            "stock": row.get('skus', '')[0].get('stock'),# 库存
        }
        data.append(tmp)
    return data

main()
# 在代码的最后添加这一行
input("Press Enter to exit...")